package com.sevenwonders;

import java.util.List;
import java.util.Map;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sevenwonders.core.City;
import com.sevenwonders.core.Resource;
import com.sevenwonders.core.City.Side;
import com.sevenwonders.core.City.WonderPhase;
import com.sevenwonders.core.City.WonderPhase.Bonus;
import com.sevenwonders.util.Helper;

public class UI {
    private UI() {
        throw new AssertionError();
    }
    
    public static void setupCity(View cityView, City city, Side side, LayoutInflater inflater) {
        TextView cityBonus = (TextView) cityView.findViewById(R.id.view_citybonus);
        cityBonus.setBackgroundResource(city.getStartingBonusId());
        TextView cityName = (TextView) cityView.findViewById(R.id.view_cityname);
        cityName.setText(city.getName());
        TextView citySide = (TextView) cityView.findViewById(R.id.view_cityside);
        citySide.setText("(" + side.getLetter() + ")");

        LinearLayout wonderPhases = (LinearLayout) cityView.findViewById(R.id.layout_wonderphases);
        Map<Side, List<WonderPhase>> phaseMap = city.getWonderPhases();
        List<WonderPhase> phaseList = phaseMap.get(side);

        for (WonderPhase phase : phaseList) {
            LinearLayout phaseLayout = (LinearLayout) inflater.inflate(R.layout.view_wonderphase, wonderPhases, false);
            LinearLayout resourceLayout1 = (LinearLayout) phaseLayout.findViewById(R.id.layout_phase_resources1);
            List<Resource> cost = phase.getCost();
            MarginLayoutParams rParams = new MarginLayoutParams(10, 10);

            if (cost.size() < 4) {
                for (int i = cost.size() - 1; i > -1; i--) {
                    Resource resource = cost.get(i);
                    TextView resourceView = new TextView(cityView.getContext());
                    resourceView.setLayoutParams(rParams);
                    resourceView.setBackgroundResource(resource.getIds()[0]);
                    resourceLayout1.addView(resourceView);
                }
            } else {
                LinearLayout resourceLayout2 = (LinearLayout) phaseLayout.findViewById(R.id.layout_phase_resources2);

                for (int i = cost.size() - 1; i > -1; i--) {
                    Resource resource = cost.get(i);
                    TextView resourceView = new TextView(cityView.getContext());
                    resourceView.setLayoutParams(rParams);
                    resourceView.setBackgroundResource(resource.getIds()[0]);
                    (i % 2 == 0 ? resourceLayout1 : resourceLayout2).addView(resourceView);
                }
            }

            if (!Helper.isEmpty(phase.getBonuses())) {
                LinearLayout bonusLayout = (LinearLayout) phaseLayout.findViewById(R.id.layout_phase_bonus);

                for (Bonus bonus : phase.getBonuses()) {
                    TextView bonusView = new TextView(bonusLayout.getContext());
                    bonusView.setGravity(Gravity.CENTER);
                    bonusView.setBackgroundResource(bonus.getResourceId());

                    if (!Helper.isEmpty(bonus.getDisplayText())) {
                        bonusView.setText(bonus.getDisplayText());
                        bonusView.setTextSize(12);
                        bonusView.setTextColor(bonus.getTextColor());
                    }

                    bonusLayout.addView(bonusView);
                }
            }

            wonderPhases.addView(phaseLayout);
        }
    }
}
